# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)
library(viridisLite)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Just in case something masks 'source()', use extra deliberate base::source().
base::source("~/code/0-utility-functions/table-and-text-utils.R", local = TRUE)

# READ IN OUR DATA -------------------------------------------------------------

moved_tract_local_labor_dt <-
    readRDS(
        "~/estimation-output/event_study_estimates_moved_tract_local_labor_markets.rds" # nolint
    )

# Map coefficients into shares (since they decompose the total moving effect)
moved_tract_local_labor_dt[
    ,
    share := (estimate / sum(estimate)),
    by = .(attribute_label)
]

# Reordering labels and cleaning up
moved_tract_local_labor_dt[
    ,
    valence := factor(valence, levels = c("Less", "More"))
]
setorderv(
    moved_tract_local_labor_dt,
    c("attribute_label", "valence"),
    order = c(1L, -1L)
)
moved_tract_local_labor_dt[
    ,
    segment_label := cumsum(share) - (0.5 * share),
    by = .(attribute_label)
]

# Reordering labels and cleaning up
moved_tract_local_labor_dt[, header_group := "Local Labor Market Attributes"]
moved_tract_local_labor_dt[
    attribute_label == "Job Growth Rate",
    attribute_label := "Job\nGrowth Rate"
]
moved_tract_local_labor_dt[
    attribute_label == "High-Paying Jobs",
    attribute_label := "High-Paying\nJobs"
]
moved_tract_local_labor_dt[
    attribute_label == "Employment Rate",
    attribute_label := "Employment\nRate"
]
moved_tract_local_labor_dt[
    attribute_label == "Population Density",
    attribute_label := "Population\nDensity"
]

moved_tract_local_labor_dt[
    ,
    attribute_label :=
        factor(
            attribute_label,
            levels = rev(
                c(
                    "Wage Growth",
                    "Job\nGrowth Rate",
                    "Job Density",
                    "Total Jobs",
                    "High-Paying\nJobs",
                    "Employment\nRate",
                    "Short Commute",
                    "Commute Time",
                    "Population\nDensity"
                )
            )
        )
]

bar_fill <- rev(viridis(n = 2))
bar_colors <- rev(bar_fill)
overall_mean_more <-
    fmt_fixed_decimal(
        input = mean(moved_tract_local_labor_dt[valence == "More"]$share),
        sub_for_missing = "-",
        should_be_rounded = TRUE,
        round_digits = 2L,
        decimal_digits = 2L,
        scale_factor = NA,
        add_comma = FALSE
    )


figure_5_4 <-
    ggplot(
        data = moved_tract_local_labor_dt,
        aes(x = attribute_label, y = share, fill = valence)
    ) +
    geom_bar(stat = "identity") +
    theme_bw(base_size = 10) +
    scale_y_continuous(
        breaks = (seq.int(from = 0, to = 100, by = 10) / 100),
        sec.axis = sec_axis(~.,
            breaks = as.numeric(overall_mean_more),
            labels = overall_mean_more
        ),
        expand = expansion(mult = c(0.005, 0.005))
    ) +
    labs(x = NULL, y = "Decomposition of Move by Destination Characteristics") +
    coord_flip(ylim = c(0, 1)) +
    facet_grid(
        header_group ~ .,
        space = "free",
        scales = "free",
        switch = "y"
    ) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank(),
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines"),
        strip.placement = "outside",
        panel.spacing.y = unit(0, "cm"),
        strip.text.y = element_text(angle = 270, size = 8),
        panel.border = element_blank(),
        axis.line = element_line(),
        axis.text.y = element_text(hjust = 0.5)
    ) +
    scale_fill_manual(values = bar_fill) +
    scale_color_manual(values = bar_colors) +
    guides(fill = guide_legend(, reverse = TRUE)) +
    geom_hline(
        yintercept = as.numeric(overall_mean_more),
        color = "#999999",
        linewidth = 1.5
    )

ggsave(
    plot = figure_5_4,
    filename = "~/paper/figures/figure-5.4.png",
    width = 6,
    height = 4.5,
    dpi = 600
)

ggsave(
    plot = figure_5_4,
    filename = "~/paper/figures/figure-5.4.tif",
    width = 6,
    height = 4.5,
    device = "tiff",
    dpi = 600
)

# Housekeeping
moved_tract_local_labor_dt <- NULL
bar_fill <- NULL
bar_colors <- NULL
overall_mean_more <- NULL
figure_5_4 <- NULL
rm(
    moved_tract_local_labor_dt,
    bar_fill,
    bar_colors,
    overall_mean_more,
    figure_5_4
)
